


/**
 * 滑动类
 * 改成了函数式类，因为无法解决window.addEventListener和当前类的this指向问题
 * 用bind(this)的话又会造成window.removeEventListener失效
 * @param {*} options 
 */

function MyMovex(options) {

    var myMovex = {
        x1:0,
        y1:0,
        startMovetime:0,
        isMovex:false,
        distance:207,
        moveCallback:null,
    };

    Object.assign(myMovex,options);

    function touchstart(e) {
        myMovex.x1 = e.changedTouches[0].pageX;
        myMovex.y1 = e.changedTouches[0].pageY;
        myMovex.isMovex = 0;
        myMovex.startMovetime = e.timeStamp;
    }

    function touchmove(e) {
        let x2 = e.changedTouches[0].pageX;
        let y2 = e.changedTouches[0].pageY;

        //判断是否是左右滑动
        let movex = Math.abs(x2-myMovex.x1);
        let movey = Math.abs(y2-myMovex.y1);

        //上下滑动的距离大于20，认为不是左右滑动
        if(movey > 20) {
            return;
        }
        //上下滑动的趋势比左右滑动更大，认为不是左右滑动
        if(movey > movex) {
            return;
        }
        myMovex.isMovex = 1;
        if(x2 > myMovex.x1) {
            //向右滑动（过渡）
            myMovex.moveCallback({deirection:'right',status:'transition',movex});
        } else {
            //向左滑动（过渡）
            myMovex.moveCallback({deirection:'left',status:'transition',movex});
        }
    }

    function touchend(e) {
        if(!myMovex.isMovex) {
            return;
        }
        let x2 = e.changedTouches[0].pageX;
        let movex = Math.abs(x2-myMovex.x1);
        let endMovetime = e.timeStamp;

        //如果在150毫秒内滑动了50px，则认为是快速滑动，这个意味着用户想要直接切换页面
        if(myMovex.isMovex && movex>=50 && (endMovetime - myMovex.startMovetime) <= 150) {
            if(x2 > myMovex.x1) {
                //向右滑动成功
                myMovex.moveCallback({deirection:'right',status:'success'});
            } else {
                //向左滑动成功
                myMovex.moveCallback({deirection:'left',status:'success'});
            }
            return;
        }
        if(movex < myMovex.distance) {
            if(x2 > myMovex.x1) {
                //向右滑动过渡失败
                myMovex.moveCallback({deirection:'right',status:'reset'});
            } else {
                //向左滑动过渡失败
                myMovex.moveCallback({deirection:'left',status:'reset'});
            }
            return;
        }
        if(x2 > myMovex.x1) {
            //向右滑动过渡成功
            myMovex.moveCallback({deirection:'right',status:'success'});
        } else {
            //向左滑动过渡成功
            myMovex.moveCallback({deirection:'left',status:'success'});
        }
    }

    function startListening() {
        window.addEventListener('touchstart',touchstart,false);
        window.addEventListener('touchmove',touchmove,false);
        window.addEventListener('touchend',touchend,false);
    }

    function stopListening() {
        window.removeEventListener('touchstart',touchstart,false);
        window.removeEventListener('touchmove',touchmove,false);
        window.removeEventListener('touchend',touchend,false);
    }

    myMovex.startListening = startListening;
    myMovex.stopListening = stopListening;
    
    return myMovex;
}

export default MyMovex;